package com.lt.basic.project.application.service.mino.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lt.basic.project.application.mapper.mino.user.UserMapper;
import com.lt.basic.project.application.service.mino.user.IUserService;
import com.lt.basic.project.common.core.PageResult;
import com.lt.basic.project.common.enums.ErrorEnum;
import com.lt.basic.project.common.exception.BaseException;
import com.lt.basic.project.common.util.ConvertUtils;
import com.lt.basic.project.pojo.entity.mino.user.User;
import com.lt.basic.project.pojo.validate.commons.IdValidate;
import com.lt.basic.project.pojo.validate.commons.PageValidate;
import com.lt.basic.project.pojo.validate.mino.user.UserCreateValidate;
import com.lt.basic.project.pojo.validate.mino.user.UserSearchValidate;
import com.lt.basic.project.pojo.validate.mino.user.UserUpdateValidate;
import com.lt.basic.project.pojo.vo.mino.user.UserDetailVo;
import com.lt.basic.project.pojo.vo.mino.user.UserListVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 用户信息表业务接口实现
 * @author LT
 * @version 1.0.0 2023-11-27
 */
@Slf4j
@Service
@Transactional
public class UserServiceImpl implements IUserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 转换VO数据
     * @param user 数据对象
     * @return UserResponse 返回Response对象
     */
    public UserDetailVo getConvertResponse(User user){
        UserDetailVo detailVo = ConvertUtils.convert(user, UserDetailVo.class);
        return detailVo;
    }

    /**
     * 验证数据
     *
     * @param id 数据id
     * @return
     */
    private User verifyData(Long id){
    User user = userMapper.selectById(id);
        if(null == user) {
            throw new BaseException(ErrorEnum.DATA_ERROR.getCode(), ErrorEnum.DATA_ERROR.getMsg());
        }
        return user;
    }

    /**
     * 根据id获取一条VO数据
     * @param id 主键参数
     * @return UserDetailVo
     */
    @Override
    public UserDetailVo detail(Long id) {
        User user = this.verifyData(id);
        return this.getConvertResponse(user);
    }

    /**
     * 获取列表数据
     * @param searchValidate 查询条件
     * @param pageValidate 分页条件
     * @return PageResult<UserListVo> 响应分页数据
     */
    @Override
    public PageResult<UserListVo> list(PageValidate pageValidate, UserSearchValidate searchValidate) {
        //带SystemAdminRequest类的多条件查询
        LambdaQueryWrapper<User> lqw = Wrappers.lambdaQuery();
        searchValidate.query(searchValidate, lqw);
        IPage<User> iPage = userMapper.selectPage(new Page<>(pageValidate.getPageNo(), pageValidate.getPageSize()), lqw);
        List<UserListVo> list = new ArrayList<>();
        iPage.getRecords().forEach(user -> {
            UserListVo vo = ConvertUtils.convert(this.getConvertResponse(user), UserListVo.class);
            list.add(vo);
        });
        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 保存
     * @param createValidate 请求参数
     */
    @Override
    public void add(UserCreateValidate createValidate) {
        User user = new User();
        user.create(createValidate);
        userMapper.insert(user);
    }

    /**
     * 编辑
     * @param updateValidate 请求参数
     */
    @Override
    public void edit(UserUpdateValidate updateValidate) {
        User user = this.verifyData(updateValidate.getId());
        user.modify(updateValidate);
        userMapper.updateById(user);
    }

    /**
     * 删除数据
     * @param idValidate 请求参数
     */
    @Override
    public void del(IdValidate idValidate) {
        User user = this.verifyData(idValidate.getId());
        userMapper.updateById(user);
    }

    @Override
    public String getName(Long id) {
        if (Objects.isNull(id)){
            return null;
        }
        return userMapper.selectOne(new QueryWrapper<User>().select("real_name").eq("id", id)).getRealName();
    }
}